﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Globalization;

namespace Medianamik.UI.Controls
{
    public class HttpRedirectSelector : BaseMedianamikControl
    {
        private readonly RadioButtonList _rbl = new RadioButtonList();
        private readonly ListItem[] _items = new[]
        {
            new ListItem(Translator.Translate("medianamik_controls", "301MovedPermanently"),"301") { Selected=true},
            new ListItem(Translator.Translate("medianamik_controls", "302Found"), "302"),
        };

        public override object GetValue()
        {
            EnsureChildControls();
            return _rbl.SelectedValue;
        }

        public override void SetValue(object value)
        {
            EnsureChildControls();
            _rbl.SelectedValue = value.ToString();
        }

        public override Type TypeOfValue
        {
            get { return typeof(string); }
        }

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            Controls.Add(_rbl);
            _rbl.ID = ControlToValidateId;

            if (pUseDataSource)
            {
                _rbl.Items.AddRange(_items);
                var value = DataSource as string ?? string.Empty;
                bool exists = _rbl.Items.Cast<ListItem>().Any(item => string.Equals(item.Value, value, StringComparison.InvariantCultureIgnoreCase));
                if (exists)
                {
                    _rbl.SelectedValue = value;
                }
            }
        }
    }
}
